package cn.example.base.dao;

import cn.example.base.bean.BaseBean;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * Created by WuYB on 2017/11/23.
 */
@Mapper
@Repository
public interface BaseDAO {

    @Options(useGeneratedKeys = true)
    @Insert("<script>" +
            "INSERT INTO ${table} " +
            "<foreach item='map' index='index' collection='keyValues' open='(' separator=',' close=')'>${map.key}</foreach>" +
            "VALUES " +
            "<foreach item='map' index='index' collection='keyValues' open='(' separator=',' close=')'>#{map.value}</foreach>" +
            "</script>")
    int insert(BaseBean baseBean);

    @Update("<script>" +
            "UPDATE ${table} " +
            "<set>" +
            "<foreach item='map' index='index' collection='keyValues' open='' separator=',' close=''>" +
            "${map.key} = #{map.value}" +
            "</foreach>" +
            "</set> " +
            "WHERE ${primaryKey}=#{primaryValue}" +
            "</script>")
    int update(BaseBean baseBean);

    @Delete("DELETE FROM ${table} WHERE ${key} = #{value}")
    int delete(@Param("table") String table, @Param("key") String key, @Param("value") String value);

    @ResultType(value = Map.class)
    @Select("SELECT * FROM ${table} WHERE ${key} = #{value}")
    Map<String, Object> get(@Param("table") String table, @Param("key") String key, @Param("value") String value);
}
